package com.cyf.wechat.service.impl;

import java.net.URLEncoder;
import java.util.Date;

import com.cyf.db.mapper.ApiUserMapper;
import com.cyf.util.DateUtil;
import com.cyf.util.HttpsUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cyf.wechat.service.WechatSendService;

@Service
public class WechatSendServiceImpl implements WechatSendService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ApiUserMapper apiUserMapper;

    @Override
    public void carIn(Integer apiUserId, String openId, String plate, String parking, String time) {
        try {
            String url = "http://wechat.cheyifu2016.com/payAdmission.php?openid="
                    + openId
                    + "&carNo="
                    + plate
                    + "&carPlace="
                    + parking
                    + "&time="
                    + URLEncoder.encode(time, "UTF-8")
                    + "&apiUserId="
                    + apiUserId;
            String resultStr = HttpsUtil.get(url);
            if (resultStr == null || !resultStr.contains("int(0)")) {
                logger.error("给微信公众号发送车辆入场信息失败：{}", resultStr);
            }
        } catch (Exception e) {
            logger.error("服务器异常 {}", e);
        }
    }

    @Override
    public void pay(Integer apiUserId, String plate, String parking, String openid, String payTime, Integer fee) {
        try {
            String url = "http://wechat.cheyifu2016.com/payNotification.php?openid="
                    + openid
                    + "&carNo="
                    + plate
                    + "&carPlace="
                    + parking
                    + "&payTime="
                    + URLEncoder.encode(payTime, "UTF-8")
                    + "&fee="
                    + fee
                    + "&apiUserId="
                    + apiUserId;
            String resultStr = HttpsUtil.get(url);
            if (resultStr == null || !resultStr.contains("int(0)")) {
                logger.error("给微信公众号发送支付通知失败：{}", resultStr);
            }
        } catch (Exception e) {
            logger.error("给微信公众号发送支付通知异常 {}", e);
        }
    }

    @Override
    public void carOut(Integer apiUserId, String openid, String plate, String parking, String stayTime,
                       String outTime, Integer fee) {
        try {
            String url = "http://wechat.cheyifu2016.com/carOutNotification.php?openid="
                    + openid
                    + "&plate="
                    + plate
                    + "&parking="
                    + parking
                    + "&stayTime="
                    + URLEncoder.encode(stayTime, "UTF-8")
                    + "&outTime="
                    + URLEncoder.encode(outTime, "UTF-8") + "&fee=" + fee
                    + "&apiUserId="
                    + apiUserId;
            String resultStr = HttpsUtil.get(url);
            if (resultStr == null || !resultStr.contains("int(0)")) {
                logger.error("给微信公众号发送车辆出场信息失败：{}", resultStr);
            }
        } catch (Exception e) {
            logger.error("给微信公众号发送车辆出场信息异常 {}", e);
        }
    }

    @Override
    public boolean sendExpiredWarning(Integer apiUserId, String openid, String plate, String parking,
                                      String endTime, String cardNo) {
        boolean retVal = false;
        try {

            String url = "http://wechat.cheyifu2016.com/expiredWarning.php?openid="
                    + openid
                    + "&parking=" + parking
                    + "&endTime=" + URLEncoder.encode(endTime, "UTF-8")
                    + "&plate=" + plate
                    + "&cardNo=" + cardNo
                    + "&apiUserId="
                    + apiUserId;
            String resultStr = HttpsUtil.get(url);
            if (resultStr == null || !resultStr.contains("int(0)")) {
                logger.error("给微信公众号发送车辆出场信息失败：{}", resultStr);
            }
//            logger.info("url=" + url);
//            CloseableHttpClient closeableHttpClient = HttpClients
//                    .createDefault();
//            HttpGet get = new HttpGet(url);
//            CloseableHttpResponse rsp = closeableHttpClient.execute(get);
//            try {
//                StatusLine statusLine = rsp.getStatusLine();
//                if (statusLine.getStatusCode() == 200) {
//                    retVal = true;
//                }
//            } catch (Exception e) {
//                logger.error("读取http请求的返回结果异常：{}", e);
//            } finally {
//                rsp.close();
//            }
        } catch (Exception e) {
            logger.error("给微信公众号发送车过期提醒服务器异常 {}", e);
        }
        return retVal;
    }

    @Override
    public void sendStatisticalData(String openid, String deptInfo, int employeeInNum, int employeeOutNum, String time) {
        try {
            String url = "http://wechat.cheyifu2016.com/statisInfo.php?openid="
                    + openid
                    + "&deptInfo="
                    + URLEncoder.encode(deptInfo, "UTF-8")
                    + "&employeeInNum="
                    + employeeInNum
                    + "&employeeOutNum="
                    + employeeOutNum
                    + "&time="
                    + URLEncoder.encode(time, "UTF-8");
            String resultStr = HttpsUtil.get(url);
            if (resultStr == null || !resultStr.contains("int(0)")) {
                logger.error("给微信公众号发送<人管平台统计消息>失败：{}", resultStr);
            }
        } catch (Exception e) {
            logger.error("给微信公众号发送人管平台统计消息异常 {}", e);
        }
    }

    @Override
    public void invite(Integer apiUserId, String openid, String intervieweeMobile, Date inviteTime) {
        try {
            String webPageAuthDomainName = apiUserMapper.getWebPageAuthDomainNameById(apiUserId);
            String inviteTimeStr = "";
            if (inviteTime != null) {
                inviteTimeStr = DateUtil.formatDate(inviteTime, "yyyy-MM-dd HH:mm");
            }
            String url = "http://" + webPageAuthDomainName + "/visitor/inviteNotification.php?openid="
                    + openid
                    + "&intervieweeMobile="
                    + intervieweeMobile
                    + "&inviteTime="
                    + URLEncoder.encode(inviteTimeStr, "UTF-8")
                    + "&apiUserId=" + apiUserId;
            String resultStr = HttpsUtil.get(url);
            if (resultStr == null || !resultStr.contains("int(0)")) {
                logger.error("给微信公众号发送<受访者邀请访客通知>失败：{}", resultStr);
            }
        } catch (Exception e) {
            logger.error("给微信公众号发送<受访者邀请访客通知>异常 {}", e);
        }
    }

    @Override
    public void applyVisit(Integer apiUserId, String openid, String name, String memo, int visitorRecordId) {
        try {
            String webPageAuthDomainName = apiUserMapper.getWebPageAuthDomainNameById(apiUserId);
            String url = "http://" + webPageAuthDomainName + "/visitor/applyVisitNotification.php?openid="
                    + openid
                    + "&name="
                    + URLEncoder.encode(name, "UTF-8")
                    + "&memo="
                    + URLEncoder.encode(memo, "UTF-8")
                    + "&visitorRecordId="
                    + visitorRecordId
                    + "&apiUserId=" + apiUserId;
            String resultStr = HttpsUtil.get(url);
            if (resultStr == null || !resultStr.contains("int(0)")) {
                logger.error("给微信公众号发送<访客来咨询通知>失败：{}", resultStr);
            }
        } catch (Exception e) {
            logger.error("给微信公众号发送<访客来咨询通知>异常 {}", e);
        }
    }

    @Override
    public void visitPass(String intervieweeName, Integer apiUserId, String openid, String visitorName, String intervieweeMobile, String estimateVisitorTime, String endTime, String orderNo) {
        try {
            String webPageAuthDomainName = apiUserMapper.getWebPageAuthDomainNameById(apiUserId);
            String url = "http://" + webPageAuthDomainName + "/visitor/visitPassNotification.php?openid="
                    + openid
                    + "&visitorName="
                    + URLEncoder.encode(visitorName, "UTF-8")
                    + "&intervieweeMobile="
                    + URLEncoder.encode(intervieweeMobile, "UTF-8")
                    + "&estimateVisitorTime="
                    + URLEncoder.encode(estimateVisitorTime, "UTF-8")
                    + "&endTime="
                    + URLEncoder.encode(endTime, "UTF-8")
                    + "&apiUserId="
                    + apiUserId
                    + "&intervieweeName="
                    + URLEncoder.encode(intervieweeName, "UTF-8")
                    + "&orderNo="
                    + orderNo;
            String resultStr = HttpsUtil.get(url);
            if (resultStr == null || !resultStr.contains("int(0)")) {
                logger.error("给微信公众号发送<访客来咨询通知>失败：{}", resultStr);
            }
        } catch (Exception e) {
            logger.error("给微信公众号发送<访客来咨询通知>异常 {}", e);
        }
    }

    @Override
    public void visitReject(String intervieweeName, Integer apiUserId, String openid, String visitorName, String intervieweeMobile, String estimateVisitorTime, String endTime, String rejectMemo, String visitMemo) {
        try {
            String webPageAuthDomainName = apiUserMapper.getWebPageAuthDomainNameById(apiUserId);
            String url = "http://" + webPageAuthDomainName + "/visitor/visitRejectNotification.php?openid="
                    + openid
                    + "&visitorName="
                    + URLEncoder.encode(visitorName, "UTF-8")
                    + "&intervieweeMobile="
                    + URLEncoder.encode(intervieweeMobile, "UTF-8")
                    + "&estimateVisitorTime="
                    + URLEncoder.encode(estimateVisitorTime, "UTF-8")
                    + "&endTime="
                    + URLEncoder.encode(endTime, "UTF-8")
                    + "&apiUserId="
                    + apiUserId
                    + "&intervieweeName="
                    + URLEncoder.encode(intervieweeName, "UTF-8")
                    + "&rejectMemo="
                    + URLEncoder.encode(rejectMemo, "UTF-8")
                    + "&visitMemo="
                    + URLEncoder.encode(visitMemo, "UTF-8");
            String resultStr = HttpsUtil.get(url);
            if (resultStr == null || !resultStr.contains("int(0)")) {
                logger.error("给微信公众号发送<预约拒绝提醒>失败：{}", resultStr);
            }
        } catch (Exception e) {
            logger.error("给微信公众号发送<预约拒绝提醒>异常 {}", e);
        }
    }
}
